package com.example.transactionhintassistant.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.transactionhintassistant.pojo.dto.memo.AddMemoDto;
import com.example.transactionhintassistant.pojo.dto.memo.ChangeMemoDto;
import com.example.transactionhintassistant.pojo.entity.Memo;
import com.example.transactionhintassistant.service.MemosService;
import com.example.transactionhintassistant.mapper.MemosMapper;
import com.example.transactionhintassistant.utils.JwtHelper;
import com.example.transactionhintassistant.utils.result.Result;
import jakarta.servlet.http.HttpServletRequest;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
* @author 86147
* @description 针对表【memos(备忘录表)】的数据库操作Service实现
* @createDate 2024-03-04 22:32:13
*/
@Service
public class MemosServiceImpl extends ServiceImpl<MemosMapper, Memo>
    implements MemosService{

    @Autowired
    private MemosMapper memosMapper;
    @Autowired
    private HttpServletRequest request;
    @Autowired
    private JwtHelper jwtHelper;

    @Override
    public Result<Object> getMyMemos()
    {
        // 获取请求头中的令牌
        String token = request.getHeader("Authorization");
        int userId = jwtHelper.getUserId(token);
        LambdaQueryWrapper<Memo> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(Memo::getUserId,userId);
        List<Memo> memosList = memosMapper.selectList(lambdaQueryWrapper);
        return Result.ok(memosList,"返回个人备忘录列表.");
    }

    @Override
    public Result<Object> deleteMemoById(int id) {
        memosMapper.deleteById(id);
        return Result.ok(null,"删除成功");
    }

    @Override
    public Result<Object> changeMyMemo(ChangeMemoDto changeMemoDto) {
        LambdaUpdateWrapper<Memo> lambdaUpdateWrapper = new LambdaUpdateWrapper();
        lambdaUpdateWrapper.eq(Memo::getId,changeMemoDto.getId())
                .set(Memo::getTitle,changeMemoDto.getTitle())
                .set(Memo::getContent,changeMemoDto.getContent());
        this.update(lambdaUpdateWrapper);
        return Result.ok(null,"修改备忘录信息成功");
    }

    @Override
    public Result<Object> addMemo(AddMemoDto addMemoDto) {
        // 获取请求头中的令牌
        String token = request.getHeader("Authorization");
        int userId = jwtHelper.getUserId(token);
        Memo newMemo = new Memo();
        BeanUtils.copyProperties(addMemoDto,newMemo);
        newMemo.setUserId(userId);
        newMemo.setTime(new Date());
        this.save(newMemo);
        return Result.ok(null,"增加备忘录成功");
    }

    @Override
    public Result<Object> getMemoDetailById(int id) {
        LambdaQueryWrapper<Memo> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(Memo::getId,id);
        Memo memo = memosMapper.selectOne(lambdaQueryWrapper);
        return Result.ok(memo,"返回单个便签信息");
    }


}




